﻿(function ($) {
	
	$.smarlyContent = function (el, options) {
		var base = this;
		base.$el = $(el);
		base.el = el;

		base.$el.data("smarlyContent", base);

		base.init = function() {
			base.options = $.extend(true, {}, $.smarlyContent.defaultOptions, options);
			base.options.$content = $('#main-content').parent();

			$('body').css('overflow-y', 'hidden');

			$('.main-navigation input[type="checkbox"]').change(function () {
				var $ch = $(this);
				var block = { display: 'none' };
				if ($ch.is(':checked')) {
					block.display = 'block';
				}
				$(this).nextAll().filter($('ul')).css(block);
			});
			
			base.options.$content.css({ top: 0, right: 0, bottom: 0, left: 0, 'z-index': 999 });


			$('#full-screen').on('click', function (e) {
				if (isFullScreen()) {
					base.options.$content.removeClass('absolute');
					$('div.block-content').getNiceScroll().show();
				} else {
					$('div.block-content').not('#main-content').getNiceScroll().hide();
					base.options.$content.addClass('absolute');
				}
				$(window).trigger('resize', true);
			});


			reCalc();
			
			$('div.block-content').niceScroll({
				cursorcolor: '#B86030',
				cursorfixedheight: 100,
				zindex: 10,
				touchbehavior: false,
				autohidemode: false,
				boxzoom: false,
			});
			

		};

		base.init();

		function onResize() {
			$(window).off('resize', onResize);
			$(window).off('scroll', onResize);

			setTimeout(function () {
				reCalc();
			}, 200);
		}

		function isFullScreen() {
			return base.options.$content.hasClass('absolute');
		}

		function reCalc() {

			$('div.block-white').css('height', function () {
				return $(window).height() 
					- $(this).offset().top 
					- (isFullScreen() ? 20 : 30);
			});
			
			$('div.block-white div.block-content').css('height', function () {
				var top = $(this).position().top;
				return $(this).parent().height() - (top > 20 ? top + 10 : top) - (isFullScreen() ? 10 : 0);
			});

			$(window).on('resize', onResize);
			$(window).on('scroll', onResize);
		}
	};

	$.smarlyContent.defaultOptions = {
		content: '.main-content',
	};


	$.fn.smarlyContent = function (options) {
		return new $.smarlyContent(this, options);
	};
})(jQuery);
